//
//  BLTemplateRectHelper.m
//  TSDemo_ImageFilter
//
//  Created by ciyouzen on 2014/5/8.
//  Copyright (c) 2014年 dvlproad. All rights reserved.
//

#import "BLTemplateRectHelper.h"
#import "CJTemplateRectHelper.h"

@implementation BLTemplateRectHelper

/*
 *  根据指定参数获取/创建拼图模型(此时还未获取到模型在真正坐标系下的真实坐标等信息)
 *
 *  @param subViewCount             样式的子框个数
 *  @param styleIndex               对应子框个数下的样式类型
 *
 *  @return 包含dictionary信息的拼图模型
 */
+ (CJTemplateModel *)templateModelWithSubViewCount:(NSInteger)subViewCount styleIndex:(NSInteger)styleIndex
{
    NSDictionary *templateDictionary = [self templateDictionaryForSubViewCount:subViewCount styleIndex:styleIndex];
    
    CJTemplateModel *templateModel = [[CJTemplateModel alloc] initWithTemplateDictionary:templateDictionary];
    
    NSString *templateTitle = templateDictionary[@"title"];
    templateModel.title = templateTitle;
    
    return templateModel;
}

/*
 *  完善模型在真正坐标系下的真实坐标等信息
 *
 *  @param templateModel            要完善的模型
 *  @param templateRealSuperFrame   模型所在的真正坐标系
 */
+ (void)improveTemplateModel:(CJTemplateModel *)templateModel
          withRealSuperFrame:(CGRect)templateRealSuperFrame
{
    NSDictionary *templateDictionary = templateModel.templateDictionary;
    
    NSMutableArray<CJTemplatePositionModel *> *realTemplateModels = [self getRealTemplateModelsWithTemplateDictionary:templateDictionary inTemplateRealSuperFrame:templateRealSuperFrame];
    templateModel.realTemplateModels = realTemplateModels;
    
    NSString *templateTitle = templateDictionary[@"title"];
    templateModel.title = templateTitle;
}

/*
 *  获取样式信息
 *
 *  @param subViewCount     样式的子框个数（最小为1）
 *  @param styleIndex       对应子框个数下的样式类型
 *
 *  @return 样式信息(含样式的frame和其上的各个point)
 */
+ (NSDictionary *)templateDictionaryForSubViewCount:(NSInteger)subViewCount styleIndex:(NSInteger)styleIndex {
    if (subViewCount < 1) {
        return nil;
    }
    NSString *styleFileName = [NSString stringWithFormat:@"number%02zd_style%02zd.plist", subViewCount, styleIndex];
    
    //NSBundle *podBundle = [NSBundle mainBundle];
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSURL *url = [bundle URLForResource:@"CQSplicedTemplateKit" withExtension:@"bundle"];
    NSBundle *podBundle = [NSBundle bundleWithURL:url];
    
    NSString *file = [podBundle.resourcePath stringByAppendingPathComponent:styleFileName];
    NSDictionary *templateDictionary = [NSDictionary dictionaryWithContentsOfFile:file];
    
    return templateDictionary;
}

/*
 *  获取模型在真正坐标系下的真实坐标等信息
 *
 *  @param templateDictionary       模型的字典信息
 *  @param templateRealSuperFrame   模型所在的真正坐标系
 *
 *  @return 模型在真正坐标系下的真实坐标等信息
 */
+ (NSMutableArray<CJTemplatePositionModel *> *)getRealTemplateModelsWithTemplateDictionary:(nullable NSDictionary *)templateDictionary                                                               inTemplateRealSuperFrame:(CGRect)templateRealSuperFrame
{
    NSMutableArray<CJTemplatePositionModel *> *realTemplateModels = [[NSMutableArray alloc] init];
    if (templateDictionary) {
        CGSize orginTemplateSuperSize = CGSizeFromString([[templateDictionary objectForKey:@"SuperViewInfo"] objectForKey:@"size"]);
        NSArray *subViewArray = [templateDictionary objectForKey:@"SubViewArray"];
        for(int i = 0; i < subViewArray.count; i++) {
            NSDictionary *subDict = [subViewArray objectAtIndex:i];
            NSArray *templatePointArray = [subDict objectForKey:@"pointArray"];
            CJTemplatePositionModel *realTemplateModel =
                [CJTemplateRectHelper realTemplateModelWithTemplatePointStrings:templatePointArray
                                                               fullTemplateSize:orginTemplateSuperSize
                                                            mapToRealSuperFrame:templateRealSuperFrame];
            
            [realTemplateModels addObject:realTemplateModel];
        }
    }
    
    return realTemplateModels;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
